package org.jlatexmath.html;

import android.graphics.Bitmap;
import android.text.Editable;
import android.text.Spannable;
import android.text.TextPaint;
import android.widget.TextView;

import org.jlatexmath.BitmapCacheUtil;
import org.jlatexmath.LaTeXInfo;
import org.jlatexmath.LaTexTextView;
import org.jlatexmath.VerticalImageSpan;
import org.sufficientlysecure.htmltextview.HttpImageGetter;

import java.util.ArrayList;

import rx.Observable;
import rx.Observer;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;


/**
 * Created by admin on 2017/7/28.
 */

public class LatexHttpImageGetter extends HttpImageGetter {


    public LatexHttpImageGetter(TextView textView) {
        super(textView);
    }

    public LatexHttpImageGetter(Editable text, TextView textView) {
        super(text, textView);
    }

    public LatexHttpImageGetter(TextView textView, boolean matchParentWidth) {
        super(textView, matchParentWidth);
    }

    @Override
    protected void invalidate(final TextView container, final Editable text) {
        final Spannable spannableString = container.getEditableText();
        final ArrayList<LaTeXInfo> laTexInfoList = ((LaTexTextView) container).getLaTexInfoList(spannableString);//
        //先加载空白图片占位
//        HtmlTagFormatter htmlTagFormatter = new HtmlTagFormatter();
//        Spanned spannable = htmlTagFormatter.handlerHtmlContent(getContext(), text, this);
//        final SpannableString spannableString = new SpannableString(spannable);
//        final SpannableString spannableString = new SpannableString(text);

        final TextPaint paint = container.getPaint();
        Observable
                .create(new Observable.OnSubscribe<Spannable>() {
                    @Override
                    public void call(Subscriber<? super Spannable> subscriber) {
                        for (int i = 0; i < laTexInfoList.size(); i++) {
                            LaTeXInfo laTeXInfo = laTexInfoList.get(i);
                            Bitmap image = BitmapCacheUtil.getInstance().getBitmapFromMemCache(laTeXInfo.getGroup() + paint.getTextSize() / paint.density);
                            if (image == null) {
                                image = ((LaTexTextView) container).getBitmap_(laTeXInfo.getTeXFormula());
                                BitmapCacheUtil.getInstance().addBitmapToMemoryCache(laTeXInfo.getGroup() + paint.getTextSize() / paint.density, image);
                            }
                            //bitmaps.add(image);
                            if (getContext() != null) {
                                spannableString.setSpan(new VerticalImageSpan(getContext(), image), laTeXInfo.getStart(), laTeXInfo.getEnd(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
                            }
                        }
                        subscriber.onNext(spannableString);
                        subscriber.onCompleted();
                    }
                })
                .subscribeOn(Schedulers.io()) // 指定 subscribe() 发生在 IO 线程(即 Observable.OnSubscribe 被激活时所处的线程。或者叫做事件产生的线程)
                .observeOn(AndroidSchedulers.mainThread()) // 指定 Subscriber 的回调发生在主线程( 或者叫做事件消费的线程。)
                .subscribe(new Observer<Spannable>() {
                    @Override
                    public void onCompleted() {
                        // int i = 0;
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(Spannable spannableString) {
                        //先加载空白图片占位
                        container.setText(spannableString);
                        LatexHttpImageGetter.super.invalidate(container, text);
                    }
                });

    }
}
